<template>
  <div class="register-wrapper">
    <el-form
      ref="registerForm"
      :model="form"
      :rules="rules"
      class="register-form"
      label-width="0"
      @keyup.enter.native="submitForm"
    >
      <h2 class="register-title">注册</h2>
      <el-form-item prop="username">
        <el-input
          placeholder="用户名"
          v-model="form.username"
          clearable
          :class="{ 'input-error': errorField === 'username' }"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          placeholder="密码"
          v-model="form.password"
          show-password
          clearable
          :class="{ 'input-error': errorField === 'password' }"
          @input="checkPasswordStrength"
        ></el-input>
        <div v-if="form.password" class="pwd-strength">
          <span :class="strengthClass">{{ strengthText }}</span>
        </div>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input
          placeholder="确认密码"
          v-model="form.confirmPassword"
          show-password
          clearable
          :class="{ 'input-error': errorField === 'confirmPassword' }"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm"
          :loading="loading"
          class="register-btn"
          style="width: 100%"
          >注册并登录</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="text" @click="login" class="login-link"
          >已有账号？登录</el-button
        >
      </el-form-item>
      <div v-if="errorMsg" class="error-msg">{{ errorMsg }}</div>
    </el-form>
  </div>
</template>
<script>
import { Register } from "../api/UserLogin";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
        confirmPassword: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          {
            required: true,
            message: "请确认密码",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              if (value !== this.form.password) {
                callback(new Error("两次输入密码不一致"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
      loading: false,
      errorMsg: "",
      errorField: "",
      strengthText: "",
      strengthClass: "",
    };
  },
  methods: {
    login() {
      this.$router.push("/login");
    },
    checkPasswordStrength() {
      const pwd = this.form.password;
      if (!pwd) {
        this.strengthText = "";
        this.strengthClass = "";
        return;
      }
      if (pwd.length < 6) {
        this.strengthText = "弱";
        this.strengthClass = "weak";
      } else if (/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/.test(pwd)) {
        this.strengthText = "中";
        this.strengthClass = "medium";
      } else if (
        /^(?=.*[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,}$/.test(
          pwd
        )
      ) {
        this.strengthText = "强";
        this.strengthClass = "strong";
      } else {
        this.strengthText = "弱";
        this.strengthClass = "weak";
      }
    },
    submitForm() {
      this.errorMsg = "";
      this.errorField = "";
      this.$refs.registerForm.validate((valid, fields) => {
        if (valid) {
          this.loading = true;
          Register({
            username: this.form.username,
            password: this.form.password,
          })
            .then((res) => {
              this.loading = false;
              const data = res.data || res;
              if (data.code === 200) {
                this.$message.success(data.msg || "注册成功");
                this.$router.push("/home");
              } else {
                this.errorMsg = data.msg || "注册失败";
                this.errorField = "username";
              }
            })
            .catch((err) => {
              this.loading = false;
              const msg = err?.response?.data?.msg || err.msg || "请求失败";
              this.errorMsg = msg;
            });
        } else {
          this.errorMsg = "请填写完整信息";
          if (fields.username) this.errorField = "username";
          else if (fields.password) this.errorField = "password";
          else if (fields.confirmPassword) this.errorField = "confirmPassword";
        }
      });
    },
  },
};
</script>
<style scoped>
.register-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fafbfc;
}
.register-form {
  width: 320px;
  padding: 32px 24px 16px 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border: 1px solid #f0f0f0;
}
.register-title {
  text-align: center;
  font-size: 22px;
  font-weight: 500;
  margin-bottom: 24px;
  color: #222;
  letter-spacing: 2px;
}
.register-btn {
  border-radius: 8px;
  font-weight: 500;
  letter-spacing: 1px;
}
.login-link {
  width: 100%;
  text-align: center;
  color: #888;
  font-size: 14px;
  margin-top: -12px;
}
.error-msg {
  color: #e74c3c;
  text-align: center;
  margin-top: 8px;
  font-size: 14px;
}
.input-error .el-input__inner {
  border-color: #e74c3c !important;
  background: #fff6f6;
}
.pwd-strength {
  margin-top: 4px;
  font-size: 13px;
  text-align: left;
}
.pwd-strength .weak {
  color: #e74c3c;
}
.pwd-strength .medium {
  color: #f39c12;
}
.pwd-strength .strong {
  color: #27ae60;
}
</style>